package com.example.springboot.controller;

import com.example.springboot.constant.ResConstansts;
import com.example.springboot.entity.Post;
import com.example.springboot.entity.Res;
import com.example.springboot.entity.vo.PostVo;
import com.example.springboot.entity.vo.ProductVo;
import com.example.springboot.service.ESService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/recommend")
public class RecommendController {
    @Autowired
    private ESService esService;
    @PostMapping("/articles")
    public Res<List<PostVo>> relatedArticles(@RequestBody Post originalArticle){
        List<PostVo> posts= esService.getRelatedArticles(originalArticle);
        if(posts == null){
            return ResConstansts.SERVICE_ERROR;
        }else{
            return new Res<List<PostVo>>().code(200).data(posts).msg("获取相关文章成功");
        }
    }
    @PostMapping("/product")
    public Res<List<ProductVo>> recommendProduct(@RequestBody Post originalArticle){
        List<ProductVo> products= esService.getRelatedProducts(originalArticle);
        if(products == null){
            return ResConstansts.SERVICE_ERROR;
        }else{
            return new Res<List<ProductVo>>().code(200).data(products).msg("获取相关商品成功");
        }
    }
}
